﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// instruction for folder sync worker
    /// </summary>
    [Serializable]
    public class FolderSyncInstruction:InstructionBase
    {
        private string _SrcFolderPath;
        /// <summary></summary>
        public string SrcFolderPath
        {
            get
            {
                return this._SrcFolderPath;
            }
            set
            {
                this._SrcFolderPath = value;
            }
        }

        private string _TargetFolderPath;
        /// <summary></summary>
        public string TargetFolderPath
        {
            get
            {
                return this._TargetFolderPath;
            }
            set
            {
                this._TargetFolderPath = value;
            }
        }

        private string _ExcludedFileExtensions;
        /// <summary></summary>
        public string ExcludedFileExtensions
        {
            get
            {
                return this._ExcludedFileExtensions;
            }
            set
            {
                this._ExcludedFileExtensions = value;
            }
        }

        private bool _IncludeSubFolders;
        /// <summary></summary>
        public bool IncludeSubFolders
        {
            get
            {
                return this._IncludeSubFolders;
            }
            set
            {
                this._IncludeSubFolders = value;
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public FolderSyncInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._SrcFolderPath = string.Empty;
            this._TargetFolderPath = string.Empty;
            this._ExcludedFileExtensions = string.Empty;
            this._IncludeSubFolders = true;
        }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="srcFolder"></param>
        /// <param name="tgtFolder"></param>
        /// <param name="filter"></param>
        /// <param name="includeSubFolders"></param>
        public FolderSyncInstruction(Guid componentID, Guid jobTicketID, 
            string srcFolder, string tgtFolder, string filter, bool includeSubFolders) 
            : base(componentID, jobTicketID)
        {
            this._SrcFolderPath = srcFolder;
            this._TargetFolderPath = tgtFolder;
            this._ExcludedFileExtensions = filter;
            this._IncludeSubFolders = includeSubFolders;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SourceFolderPath", this.SrcFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TargetFolderPath", this.TargetFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ExcludedFileExtensions", this.ExcludedFileExtensions);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubFolders", this.IncludeSubFolders.ToString());

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FolderSyncInstruction instruction = new FolderSyncInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.SrcFolderPath = xNode.Attributes.GetNamedItem("SourceFolderPath").Value;
                instruction.TargetFolderPath = xNode.Attributes.GetNamedItem("TargetFolderPath").Value;
                instruction.ExcludedFileExtensions = xNode.Attributes.GetNamedItem("ExcludedFileExtensions").Value;
                instruction.IncludeSubFolders = bool.Parse(xNode.Attributes.GetNamedItem("IncludeSubFolders").Value);

                return instruction;
            }
            else
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
        }
    }
}
